from flask import Flask
from flask_cors import CORS
from flask_jwt_extended import JWTManager
import os
import traceback
import pymysql

# flask配置
app = Flask(__name__)
CORS(app, supports_credentials=True)

# 授权配置
app.config.update(JWT_SECRET_KEY=os.urandom(24))
app.config["SECRET_KEY"] = os.urandom(24)
jwt = JWTManager(app)

# 数据库配置
mysql_param = {
    'user': "root",
    'passwd': "root",
    'db': "bangjiang",
    'host': "127.0.0.1",
    'local_infile': 1
}
con = pymysql.connect(**mysql_param)

# 缓存和文件配置
gate_info_cache = {}
car_count_temp_file_path = 'static/car-info-by-time-result.csv'
pass_info_cache = {}

gate_pass_info_file_path_placeholder = 'gate_pass_info/gate_pass_info_{}'


def get_connection():
    global con
    try:
        con.ping(reconnect=True)
        print('db is connecting')
    except:
        traceback.print_exc()
        con = pymysql.connect(**mysql_param)
        print('db reconnect')
    return con


def get_unknown_gate_info(gate_id):
    return GateInfo([gate_id, '', '', '', 0, 0, 0, 0, '', 0, 0, 0, 0, 0, '', ''])


class GateInfo:
    def __init__(self, row):
        self.gate_id = row[0]
        self.gate_name = row[1]
        self.gate_address = row[2]
        self.road_name = row[3]
        self.road_type = int(row[4])
        self.direction_type = int(row[5])
        self.direction_desc = int(row[6])
        self.gate_type = int(row[7])
        self.near = row[8]
        self.gate_longitude = row[9]
        self.gate_latitude = row[10]
        self.gate_state = row[11]
        self.trigger_type = row[12]
        self.road_count = row[13]
        self.start_time = row[14]
        self.end_time = row[15]

    def to_array(self):
        return [self.gate_id, self.gate_name, self.gate_address, self.road_name, self.road_type, self.direction_type,
                self.direction_desc, self.gate_type, self.near, self.gate_longitude, self.gate_latitude,
                self.gate_state, self.trigger_type, self.road_count, self.start_time, self.end_time]


class GatePassInfo:
    def __init__(self, row):
        self.gate_id = row[0]
        self.unknown_id = row[1]
        self.gate_address = row[2]
        self.road_name = row[3]
        self.road_type = int(row[4])
        self.direction_type = int(row[5])
        self.direction_desc = int(row[6])
        self.gate_type = int(row[7])
        self.near = row[8]
        self.gate_longitude = row[9]
        self.gate_latitude = row[10]
        self.gate_state = row[11]
        self.trigger_type = row[12]
        self.road_count = row[13]
        self.start_time = row[14]
        self.end_time = row[15]


def load_gate_info_to_cache():
    cursor = get_connection().cursor()
    cursor.execute('select * from gate_info')
    rows = cursor.fetchall()
    for row in rows:
        gate_info = GateInfo(row)
        gate_info_cache[gate_info.gate_id] = gate_info
    print('gate_info load finish, count is {}'.format(len(gate_info_cache.keys())))


def load_data_to_cache():
    load_gate_info_to_cache()


load_data_to_cache()
